﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml;
using Microsoft.Windows.Storage.Pickers;
using System;
using System.IO;
using System.Windows.Input;
using TextNovelReader.Services;
using TextNovelReader.Views;

namespace TextNovelReader.Model;


public partial class MenuControlViewModel : ObservableObject
{
    private string _title = string.Empty;
    private string _fileName = string.Empty;
    private Visibility _fullTitleVisibility = Visibility.Collapsed;
    private Visibility _halfTitleVisibility = Visibility.Visible; 

    public MenuControlViewModel()
    {
        PickFileCommand = new RelayCommand(OnPickFileCommand_Invoked);
        AdjustFontSizeCommand = new RelayCommand<double>(OnAdjustFontSizeCommand_Invoked);
        SetWhiteLinesCommand = new RelayCommand<Int32>(OnSetWhiteLinesCommand_Invoked);
        ReaderScrollCommand = new RelayCommand<double>(OnReaderScrollCommand_Invoked);
        SetScrollSpeedCommand = new RelayCommand<Int32>(OnSetScrollSpeedCommand_Invoked);
        SetThemesCommand = new RelayCommand<Int32>(OnSetThemesCommand_Invoked);
        SetChapterIndexDeltaCommand = new RelayCommand<Int32>(OnSetChapterIndexDeltaCommand_Invoked);
        HideTitleListViewCommand = new RelayCommand<bool>(OnHideListViewCommand_Invoked);
        SetFontNameCommand = new RelayCommand<string>(OnSetFontNameCommand_Invoked); 

        if (File.Exists(GlobalServices.Settings.FilePath))
        {
            _fileName = GetFileNameFromPath(GlobalServices.Settings.FilePath); 
        }

        GlobalServices.Events.TitleChanged += UpdateTitle; 
    }

    private void OnSetFontNameCommand_Invoked(string? fontName)
    {
        if (string.IsNullOrEmpty(fontName))
            return;
        GlobalServices.Settings.FontName = fontName; 
    }

    public ICommand PickFileCommand { get; }
    public ICommand AdjustFontSizeCommand { get; }
    public ICommand SetWhiteLinesCommand { get; }
    public ICommand ReaderScrollCommand { get; }
    public ICommand SetScrollSpeedCommand { get; }
    public ICommand SetThemesCommand { get; }
    public ICommand SetChapterIndexDeltaCommand { get; }
    public ICommand HideTitleListViewCommand { get; }
    public ICommand SetFontNameCommand { get; }


    public string Title
    {
        get => _title;
        set => SetProperty(ref _title, value);
    }

    public string FileName
    {
        get => _fileName;
        set => SetProperty(ref _fileName, value); 
    }

    public Visibility FullTitleVisibility
    {
        get => _fullTitleVisibility;
        set => SetProperty(ref _fullTitleVisibility, value); 
    }

    public Visibility HalfTitleVisibility
    {
        get => _halfTitleVisibility;
        set => SetProperty(ref _halfTitleVisibility, value); 
    }

    public void UpdateTitle(string title)
    {
        Title = title; 
    }

    private void OnSetThemesCommand_Invoked(int index)
    {
        GlobalServices.Settings.ThemeIndex = index;
    }

    private void OnSetScrollSpeedCommand_Invoked(int scrollSpeed)
    {
        GlobalServices.Settings.ScrollSpeed = scrollSpeed; 
    }

    private async void OnPickFileCommand_Invoked()
    {
        FileOpenPicker picker = new(MainWindow.WindowId);
        picker.FileTypeFilter.Add(".txt");
        picker.SuggestedStartLocation = PickerLocationId.Downloads;

        PickFileResult result = await picker.PickSingleFileAsync(); 
        if(result != null)
        {
            GlobalServices.Settings.FilePath = result.Path;
            FileName = GetFileNameFromPath(result.Path); 
        }
    }

    private void OnAdjustFontSizeCommand_Invoked(double delta)
    {
        double newSize = GlobalServices.Settings.FontSize + delta;
        GlobalServices.Settings.FontSize = Math.Max(5.0, newSize); 
    }

    private void OnSetWhiteLinesCommand_Invoked(Int32 whiteLines)
    {
        GlobalServices.Settings.WhiteLines = whiteLines; 
    }

    private void OnReaderScrollCommand_Invoked(double direction)
    {
        GlobalServices.Events.RaiseReaderScrollRequested(direction); 
    }

    private void OnSetChapterIndexDeltaCommand_Invoked(int delta)
    {
        GlobalServices.Events.RaiseChapterIndexDeltaModified(delta);
    }

    private void OnHideListViewCommand_Invoked(bool hide)
    {
        GlobalServices.Events.RaiseHideListView(hide); 
        if(hide)
        {
            FullTitleVisibility = Visibility.Visible;
            HalfTitleVisibility = Visibility.Collapsed;
        }
        else
        {
            FullTitleVisibility = Visibility.Collapsed;
            HalfTitleVisibility = Visibility.Visible; 
        }
    }

    private static string GetFileNameFromPath(string path)
    {
        FileInfo fileInfo = new(path);
        var fileName = fileInfo.Name;
        var extension = fileInfo.Extension;
        return fileName[..^extension.Length]; 
    }
}
